package com.ap.core.business.message.domain.translators.impl;

import com.ap.core.business.message.domain.translators.BindingListener;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.domain.translators.Translator;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.dao.ZappConsumerDao;
import com.ap.core.dao.ZappUserDao;
import com.ap.core.dao.ZappUserVerificationDao;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.domain.ZappConsumer;
import com.ap.core.domain.ZappUser;
import com.ap.core.domain.ZappUserVerification;
import com.ap.core.message.manageconsumer.info.RequestRegisterConsumerTypeInfo;
import com.ap.core.transaction.APTransactionGeneratorUtil;
import org.joda.time.LocalDateTime;

import java.util.UUID;

/**
 * User: Ranjit.Singh
 * Implementation class for {@link com.ap.core.business.message.domain.translators.Translator} to translate RequestRegisterConsumerType bound object to Zapp Consumer domain
 */
public class ConsumerRegisterTranslator implements Translator<RequestRegisterConsumerTypeInfo> {

    private ZappConsumerDao zappConsumerDao;
    private ZappUserDao zappUserDao;
    private ZappUserVerificationDao zappUserVerificationDao;
    private FinancialInstitutionDao financialInstitutionDao;

    @Override
    public void translate(RequestRegisterConsumerTypeInfo boundObject, BindingMetaData metaData, BindingListener bindingListener) {

        final ZappUser zappUser = new ZappUser(boundObject.getRegistrationInfo().getCredentials().getEmailId(),
                                               boundObject.getRegistrationInfo().getMobNb());
        zappUserDao.persist(zappUser);

        final FinancialInstitution financialInstitution = financialInstitutionDao.getFIByIdentification(boundObject.getRegistrationInfo().getFIId());

        final ZappConsumer zappConsumer = new ZappConsumer(boundObject.getRegistrationInfo().getFIConsumerId(),
                                                           financialInstitution.getId(),
                                                           zappUser.getId());
        zappConsumerDao.persist(zappConsumer);

        final String verificationId = getUUIDGenerator() + getUUIDGenerator();

        //TODO: The expiry time stamp is current time+1day as there is already change request around this so not making it configurable.

        final ZappUserVerification zappUserVerification = new ZappUserVerification(verificationId, new LocalDateTime().plusDays(1), zappUser.getId());
        zappUserVerificationDao.persist(zappUserVerification);

        // This flush is required to have minimize window to check for failure as next step is to do LDAP transaction.
        zappUserDao.flush();
        zappConsumerDao.flush();
        zappUserVerificationDao.flush();

        bindingListener.onZappUser(zappUser, verificationId);

    }

    public void setZappConsumerDao(ZappConsumerDao zappConsumerDao) {
        this.zappConsumerDao = zappConsumerDao;
    }

    public void setZappUserDao(ZappUserDao zappUserDao) {
        this.zappUserDao = zappUserDao;
    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }

    public void setZappUserVerificationDao(ZappUserVerificationDao zappUserVerificationDao) {
        this.zappUserVerificationDao = zappUserVerificationDao;
    }

    private static String  getUUIDGenerator(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }
}